import { createApp } from 'vue'
import { createPinia } from 'pinia'

import {
  ConfigProvider,
  Button,
  Toast,
  Lazyload,
  Col, Row,
  Loading,
  NavBar,
  Form, Field,
  Cell, CellGroup,
  Checkbox, CheckboxGroup,
  Tabbar, TabbarItem,
  Card,
  DropdownMenu, DropdownItem,
  Icon,
  SwipeCell,
  Stepper,
  BackTop,
  Swipe, SwipeItem,
  Tab, Tabs,
  Empty,

  showToast,
} from 'vant'
import 'vant/lib/index.css'
import '@vant/touch-emulator'

import App from './App.vue'
import router from './router'

import './assets/main.scss'

const app = createApp(App)
const pinia = createPinia()

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title as string
  }
  // 如果某些页面需要特殊条件访问重定向
  // if (// 检查用户是否已登录
  //   !isAuthenticated &&
  //     // ❗️ 避免无限重定向
  //     to.name !== 'Login'
  // ) {
  //   // 将用户重定向到登录页面
  //   return { name: 'Login' }
  // }
  next()
})

// 为pinia挂载方法
// pinia.use()

app.config.errorHandler = (err) => {
  /* 处理错误 */
  console.warn('组件级别错误', err)
}

// 全局方法挂载
// app.config.globalProperties.$toast = showToast
app.provide('$toast', showToast)

// 全局组件挂载

app.use(pinia)
app.use(router)

// 全局vant组件挂载
app.use(ConfigProvider)
app.use(Button)
app.use(Toast)
app.use(Lazyload, {
  lazyComponent: true, // 组件懒加载包裹容器
})
app.use(Col)
app.use(Row)
app.use(Loading)
app.use(NavBar)
app.use(Form)
app.use(Field)
app.use(Cell)
app.use(CellGroup)
app.use(Checkbox)
app.use(CheckboxGroup)
app.use(Tabbar)
app.use(TabbarItem)
app.use(Card)
app.use(DropdownMenu)
app.use(DropdownItem)
app.use(Icon)
app.use(SwipeCell)
app.use(Stepper)
app.use(BackTop)
app.use(Swipe)
app.use(SwipeItem)
app.use(Tab)
app.use(Tabs)
app.use(Empty)

app.mount('#app')
